package com.hzhc.controller.charge;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.ChargeContractDetail;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.ChargeContractDetailService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.ChargeContractDetailVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/6/26 19:13
 * @description ChargeContractDetailController
 */
@RestController
@RequestMapping("/chargeContractDetail")
@RequiredArgsConstructor
public class ChargeContractDetailController extends BaseController {

    private final ChargeContractDetailService contractDetailService;

    /**
     * 计费合同详情 list
     * @param request
     * @param vo
     * @return
     */
    @RequestMapping("/getContractDetailList")
    @ResponseBody
    public ResultUtil<List<ChargeContractDetailVo>> getContractDetailList(HttpServletRequest request, @RequestBody ChargeContractDetailVo vo){
        Page<ChargeContractDetailVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<ChargeContractDetailVo> list = contractDetailService.getContractDetailList(page,vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 计费合同详情 add
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/addContractDetail")
    @SysTemLog(operModul = "计费管理-计费合同详情",operType = "ADD",operDesc = "计费合同子表添加",modular="PC")
    public ResultUtil<T> addContractDetail(HttpServletRequest request,@RequestBody ChargeContractDetailVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUserId(userID);
        vo.setCreateTime(LocalDateTime.now());
        JSONObject json=new JSONObject();
        json.put("cntrNum",vo.getCntrNum());
        json.put("efFlag",vo.getEfFlag());
        json.put("cntrType",vo.getCntrType());
        json.put("cntrSize",vo.getCntrSize());
        json.put("cntrWeight",vo.getCntrWeight());
        json.put("weight",vo.getWeight());
        json.put("carNo",vo.getCarNo());
        json.put("dayNum",vo.getDayNum());
        json.put("monthNum",vo.getMonthNum());
        json.put("restitope",vo.getRestitope());
        json.put("area",vo.getArea());
        json.put("number",vo.getNumber());
        json.put("frequency",vo.getFrequency());
        json.put("unloadingVolume",vo.getUnloadingVolume());
        String s = json.toJSONString();
        vo.setElementJson(s);
        ChargeContractDetail detail=new ChargeContractDetail();
        BeanUtils.copyProperties(vo,detail);
        return contractDetailService.addContractDetail(detail);
    }

    /**
     * 计费合同详情 update
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/updateContractDetail")
    @SysTemLog(operModul = "计费管理-计费合同详情",operType = "UPDATRE",operDesc = "计费合同子表修改",modular="PC")
    public ResultUtil<T> updateContractDetail(HttpServletRequest request,@RequestBody ChargeContractDetailVo vo){
        Long userID = TokenUtil.getUserID(request);
        vo.setUpdateUserId(userID);
        vo.setUpdateTime(LocalDateTime.now());
        JSONObject json=new JSONObject();
        json.put("cntrNum",vo.getCntrNum());
        json.put("efFlag",vo.getEfFlag());
        json.put("cntrType",vo.getCntrType());
        json.put("cntrSize",vo.getCntrSize());
        json.put("cntrWeight",vo.getCntrWeight());
        json.put("weight",vo.getWeight());
        json.put("carNo",vo.getCarNo());
        json.put("dayNum",vo.getDayNum());
        json.put("monthNum",vo.getMonthNum());
        json.put("restitope",vo.getRestitope());
        json.put("area",vo.getArea());
        json.put("number",vo.getNumber());
        json.put("frequency",vo.getFrequency());
        json.put("unloadingVolume",vo.getUnloadingVolume());
        String s = json.toJSONString();
        vo.setElementJson(s);
        ChargeContractDetail detail=new ChargeContractDetail();
        BeanUtils.copyProperties(vo,detail);
        return contractDetailService.updateContractDetail(detail);
    }

    /**
     * 计费合同详情 delete
     * @param id
     * @return
     */
    @GetMapping("/delContractDetail/{id}")
    @SysTemLog(operModul = "计费管理-计费合同详情",operType = "DELETE",operDesc = "计费合同子表删除",modular="PC")
    public ResultUtil<T> delContractDetail(@PathVariable("id") Long id){
        return contractDetailService.delContractDetail(id);
    }

}
